/*
 * @Author: yucr
 * @Date: 2020-09-02 13:24:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-08 15:58:45
 * @FilePath: \web\chu\rem.js
 */

(function (win) {
    var doc = win.document;
    var docEl = doc.documentElement;
    var tid;
    function getStyle(dom, attr) {
        if (window.getComputedStyle) {
            return window.getComputedStyle(dom, null)[attr];
        } else {
            return dom.currentStyle[attr];
        }
    }
    function fixFontSize(width, rem,) {
        var body = doc.getElementsByTagName('body')[0];
        body.style.width = '7.5rem';
        var scale = 1;
        var bodyWidth = parseInt(getStyle(body, 'width'));
        if (bodyWidth != width) {
            scale = width / bodyWidth;
            rem = rem * scale;
            docEl.style.fontSize = rem + 'px';
        }
        body.style.width = '100%';
    }
    function refreshRem() {
        var html = document.getElementsByTagName('html')[0];
        var width = parseInt(getStyle(html, 'width'));
        /*if (width > 768) {
         width = 768;
         }*/
        var rem = width / 7.5;
        docEl.style.fontSize = rem + 'px';
        fixFontSize(width, rem);
    }

    win.addEventListener('resize', function () {
        clearTimeout(tid);
        tid = setTimeout(refreshRem, 300);
    }, false);

    win.addEventListener('pageshow', function (e) {
        if (e.persisted) {
            clearTimeout(tid);
            tid = setTimeout(refreshRem, 300);
        }
    }, false);
    refreshRem();
})(window);

  // ios缩放
  window.onload = function () {
    document.addEventListener('touchstart', function (event) {
      if (event.touches.length > 1) {
        event.preventDefault();
      }
    });
    var lastTouchEnd = 0;
    document.addEventListener('touchend', function (event) {
      var now = (new Date()).getTime();
      if (now - lastTouchEnd <= 300) {
        event.preventDefault();
      }
      lastTouchEnd = now;
    }, false);
    document.addEventListener('gesturestart', function (event) {
      event.preventDefault();
    });
  }